package com.bw.yk02.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.yk02.util.MyKafkaUtil;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class BaseLogApp {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        DataStreamSource<String> ods_log_ds=env.addSource(MyKafkaUtil.getKafkaConsumer("ods_gmall_log","ods_gmall_log_group"));


        SingleOutputStreamOperator<String> ods_log_filter=ods_log_ds.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                try {
                    JSONObject jsonObject = JSON.parseObject(s);
                    if(jsonObject!=null){
                        return true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return false;
            }
        });

        OutputTag<String> start_log=new OutputTag<String>("start"){};
        OutputTag<String> display_log=new OutputTag<String>("display"){};

        SingleOutputStreamOperator<String> page_log=ods_log_filter.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {

                try {
                    JSONObject jsonObject = JSON.parseObject(s);
                    if(jsonObject.getJSONObject("start")!=null)
                    {
                        String start_log_str=jsonObject.getJSONObject("start").toJSONString();
                        context.output(start_log,start_log_str);
                    }else
                    {
                        if(jsonObject.getJSONObject("page")!=null)
                        {
                            collector.collect(jsonObject.toJSONString());
                            JSONArray display_log_array=jsonObject.getJSONArray("display");
                            if(display_log_array!=null&&display_log_array.size()>0)
                            {
                                for(Object obj :display_log_array)
                                {
                                    if(obj!=null)
                                    {
                                        JSONObject display_log_obj=JSONObject.parseObject(obj.toString());
                                        context.output(display_log,display_log_obj.toJSONString());
                                    }
                                }
                            }
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                
                
            }
        });

        DataStream<String> start_ds=page_log.getSideOutput(start_log);
        DataStream<String> display_ds=page_log.getSideOutput(display_log);

        start_ds.addSink(MyKafkaUtil.getKafkaProducer("dwd_start_log"));
        display_ds.addSink(MyKafkaUtil.getKafkaProducer("dwd_display_log"));
        page_log.addSink(MyKafkaUtil.getKafkaProducer("dwd_page_log"));



        env.execute();
    }
}
